//
//  STAddMarketVC.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/10.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STAddMarketVC.h"
#import "SWCouponMenueView.h"
#import "YLInputView.h"
#import "SWYLInputViewB.h"
#import "YLTimePickView.h"
#import "STTimerPickerView.h"

@interface STAddMarketVC ()<UITextFieldDelegate>

@property(nonatomic, strong) UIView *mainView;
@property(nonatomic, strong) UIView *mainViewB;
@property(nonatomic, assign) NSInteger type;
@property(nonatomic, strong) NSMutableArray <UITextField *>*couponMenueViewAs;
@property(nonatomic, strong) NSMutableArray <UITextField*>*couponMenueViewBs;
@property(nonatomic, strong) STTimerPickerView* timerPicekerView;
@property(nonatomic, strong) NSString *discountStart;
@property(nonatomic, strong) NSString *discountEnd;
@end

@implementation STAddMarketVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"添加优惠卷";
    
    _type = 1;
    [self loadFooterView];
    [self loadAddMarketListView];
}

-(void)loadFooterView{
    UIButton * footerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    footerBtn.backgroundColor = ButtonBackColor;
    [footerBtn setTitle:@"发布" forState:UIControlStateNormal];
    [footerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerBtn setTarget:self action:@selector(actionUpNewMarketing) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:footerBtn];
    [footerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.mas_equalTo(50);
        make.bottom.offset(0);
    }];
}

-(void)loadAddMarketListView{
    WeakSelf
    self.view.backgroundColor = UIColor.groupTableViewBackgroundColor;
    
    SWCouponMenueView *menueView = [[NSBundle mainBundle]loadNibNamed:[SWCouponMenueView className] owner:self options:nil].lastObject;
    menueView.SelectBackBlock = ^(NSInteger index) {
        weakSelf.type = index;
        if (index == 1) {
            self.mainView.hidden = NO;
            self.mainViewB.hidden = YES;
            
        }else {
            self.mainView.hidden = YES;
            self.mainViewB.hidden = NO;
        }
        
    };
    
    [self.view addSubview:menueView];
    [menueView clickAction:menueView.oneBtn];
    
    [menueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(84);
        make.left.right.inset(0);
        make.height.mas_equalTo(115);
    }];
    
    [self.view addSubview:self.mainView];
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(menueView.mas_bottom).offset(20);
        make.left.right.inset(0);
    }];
    
    [self.view addSubview:self.mainViewB];
    [self.mainViewB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(menueView.mas_bottom).offset(20);
        make.left.right.inset(0);
    }];
    
    self.mainViewB.hidden = YES;
}

-(void)actionUpNewMarketing{
     __weak typeof(self)weakSelf = self;
    NSDictionary *dict;
    if (_type == 1) {
        
        for ( UITextField *textFiled in _couponMenueViewAs) {
            if (textFiled.text.length < 1 || [textFiled.text isEqualToString:@""]) {
                [YLProgressHUDManager showError:@"请填写完善数据"];
                return;
            }
        }
        
        dict = @{@"amount":_couponMenueViewAs[0].text, @"deduction":_couponMenueViewAs[1].text,@"quantity":_couponMenueViewAs[2].text, @"validstart":_couponMenueViewAs[3].text, @"validend":_couponMenueViewAs[4].text, @"receivestart":_couponMenueViewAs[5].text, @"receiveend":_couponMenueViewAs[6].text, @"type":@1, @"stylistId":[YLUserInfo sharedInstance].userID, @"limited":@0};

      
        
    } else {
        
        for ( UITextField *textFiled in _couponMenueViewBs) {
            
            if (textFiled.text.length < 1 || [textFiled.text isEqualToString:@""]) {
                [YLProgressHUDManager showError:@"请填写完善数据"];
                
                
                NSLog(@"=== %@",textFiled.text);
                
                return;
            } else {
                NSLog(@"%@",textFiled.placeholder);
            }
        }
        
        dict = @{@"deduction":_couponMenueViewBs[0].text, @"quantity":_couponMenueViewBs[1].text,@"usestart":_discountStart, @"useend":_discountEnd, @"validstart":_couponMenueViewBs[3].text, @"validend":_couponMenueViewBs[4].text, @"receivestart":_couponMenueViewBs[5].text, @"receiveend":_couponMenueViewBs[6].text, @"type":@2, @"stylistId":[YLUserInfo sharedInstance].userID, @"limited":@0};
    }
    
    
    
    BasicRequest * request = [[BasicRequest alloc] initWithParams:dict];
    request.URL = [NSString stringWithFormat:@"%@stylistCoupon/add",BASEURL];
    request.requestMethod = HttpRequestMethodRawPOST;
    [request startRequestWithSuccess:^(NSURLSessionTask *task, id responseObject) {
        
        NSNumber *code = responseObject[@"code"];
        if (code.integerValue == 200) {
            [YLProgressHUDManager showSuccess:responseObject[@"message"]];
           
            if (weakSelf.uploadSuccessBlock) {
                weakSelf.uploadSuccessBlock();
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.75* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        }
        
    } Failure:nil];
    
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [self.view endEditing:YES];
    if (textField.tag == 10000) {
        WeakSelf
        NSMutableArray * hours = [NSMutableArray arrayWithCapacity:24];
        NSMutableArray * minutes = [NSMutableArray arrayWithCapacity:60];
        for (int i = 0; i<24; i++) {
            NSString * hour = [NSString stringWithFormat:@"%02d",i];
            [hours addObject:hour];
        }
        for (int j = 0; j<60; j++) {
            NSString * minute = [NSString stringWithFormat:@"%02d",j];
            [minutes addObject:minute];
        }
        NSArray * arrTimerSel = @[hours,minutes,hours,minutes];
        _timerPicekerView = [[STTimerPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [_timerPicekerView setTimerPickerViewWithArr:arrTimerSel withHolder:@{@"fromHour":@"01",@"fromMinute":@"01",@"endHour":@"01",@"endMinute":@"01",}];
        [self.view addSubview:_timerPicekerView];
        _timerPicekerView.selectedItemBlock = ^(NSArray *addressArr) {
            
            NSString *county1 = [addressArr objectAtIndex:0];
            NSString *county2 = [addressArr objectAtIndex:1];
            NSString *county3 = [addressArr objectAtIndex:2];
            NSString *county4 = [addressArr objectAtIndex:3];
            
            _discountStart = [NSString stringWithFormat:@"%@:%@",county1,county2];
            _discountEnd = [NSString stringWithFormat:@"%@:%@",county3,county4];
            textField.text = [NSString stringWithFormat:@"%@:%@~%@:%@",county1,county2,county3,county4];
        };
        _timerPicekerView.cancelItemBlock = ^{
            [weakSelf.timerPicekerView removeFromSuperview];
        };
        
        
        
    }else {
    YLTimePickView * pickView = [[YLTimePickView alloc] init];
    pickView.selectDateB = ^(NSString *date) {
        textField.text = date;
    };
    [pickView show];
    }
    
    return NO;
}

- (UIView *)mainView {
    if (_mainView == nil) {
        _mainView = [UIView new];
        
        
        NSArray <NSArray *>*titlArray = @[@[@"满", @"请输入满减金额"],
                                          @[@"减", @"请输入满减金额"],
                                          @[@"每日发放总数", @"请输入发放总数"]
                                          ];
        _mainView.backgroundColor = UIColor.whiteColor;
        _couponMenueViewAs = [NSMutableArray array];
        NSMutableArray <UIView *>*views =[NSMutableArray array];
        
        for (int i = 0; i < titlArray.count; i ++) {
            
            YLInputView *menueView = [[NSBundle mainBundle]loadNibNamed:[YLInputView className] owner:self options:nil].lastObject;
            menueView.titleLab.text = titlArray[i][0];
            menueView.inputField.placeholder = titlArray[i][1];
            [_couponMenueViewAs addObject:menueView.inputField];
            [views addObject:menueView];
            [_mainView addSubview:menueView];
          
            if (i == 0 || i == 1|| i == 2) {
                menueView.inputField.keyboardType = UIKeyboardTypeDecimalPad;
            }
        }
        
        NSArray *titleBs = @[@"有效时间", @"发布可领取时间"];
        
        for (int i = 0; i < titleBs.count; i ++) {
            
            SWYLInputViewB *menueView = [[NSBundle mainBundle]loadNibNamed:[SWYLInputViewB className] owner:self options:nil].lastObject;
            menueView.titleLab.text = titleBs[i];
            [views addObject:menueView];
            [self.couponMenueViewAs addObject:menueView.inputAField];
            [self.couponMenueViewAs addObject:menueView.inputBField];
            [_mainView addSubview:menueView];
            menueView.inputAField.delegate = self;
            menueView.inputBField.delegate = self;
            
        }
        UIView *oldView;
        for (int i = 0; i < views.count; i ++) {
            
            UIView *view = views[i];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.inset(20);
                if (oldView) {
                    make.top.equalTo(oldView.mas_bottom).offset(10);
                } else {
                    make.top.offset(10);
                }
                make.height.mas_equalTo(44);
                if (i == views.count - 1) {
                    make.bottom.offset(-20);
                }
            }];
            oldView = view;
        }
        
    }
    return _mainView;
}

- (UIView *)mainViewB {
    if (_mainViewB == nil) {
        _mainViewB = [UIView new];
        _mainViewB.backgroundColor = UIColor.whiteColor;
        
        _couponMenueViewBs = [NSMutableArray array];
        NSMutableArray <UIView *>*views =[NSMutableArray array];
        NSArray *titles = @[@[@"折扣", @"请输入折扣"],@[@"每天发放总数", @"请输入发放总数"], @[@"优惠时间", @""]];
        for (int i = 0; i < titles.count; i ++) {
            
            YLInputView *menueView = [[NSBundle mainBundle]loadNibNamed:[YLInputView className] owner:self options:nil].lastObject;
            menueView.titleLab.text = titles[i][0];
            menueView.inputField.placeholder = titles[i][1];
            [self.couponMenueViewBs addObject:menueView.inputField];
            [views addObject:menueView];
            [_mainViewB addSubview:menueView];
            
            if (i == 2) {
                menueView.inputField.delegate = self;
                menueView.arrowImageView.hidden = NO;
                menueView.inputField.tag  = 10000;
                menueView.inputField.textAlignment = NSTextAlignmentRight;
            }
            if (i == 0 || i == 1) {
                menueView.inputField.keyboardType = UIKeyboardTypeDecimalPad;
            }
        }
        NSArray *titleBs = @[@"有效时间选择", @"可领取时间"];
        
        for (int i = 0; i < titleBs.count; i ++) {
            
            SWYLInputViewB *menueView = [[NSBundle mainBundle]loadNibNamed:[SWYLInputViewB className] owner:self options:nil].lastObject;
            menueView.titleLab.text = titleBs[i];
            [views addObject:menueView];
            [self.couponMenueViewBs addObject:menueView.inputAField];
            [self.couponMenueViewBs addObject:menueView.inputBField];
            [_mainViewB addSubview:menueView];
            menueView.inputAField.delegate = self;
            menueView.inputBField.delegate = self;
            
            if (i == 0 || i == 1) {
                menueView.inputAField.keyboardType = UIKeyboardTypeDecimalPad;
            }
        }
        
        UIView *oldView;
        for (int i = 0; i < views.count; i ++) {
            
            UIView *view = views[i];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.inset(20);
                if (oldView) {
                    make.top.equalTo(oldView.mas_bottom).offset(10);
                } else {
                    make.top.offset(10);
                }
                make.height.mas_equalTo(44);
                if (i == views.count - 1) {
                    make.bottom.offset(-40);
                }
            }];
            oldView = view;
        }
        
    }
    return _mainViewB;
}

@end
